package com.hymjweb.demo.hymj.finance_manage.invoice_register.service;

import com.hymjweb.demo.SpringAopUtil;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.bean.CwInvoiceRegisterBean;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.bean.CwInvoiceRegisterPojo;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.detail.bean.CwInvoiceRegisterDetailPojo;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.detail.service.CwInvoiceRegisterDetailService;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.mapper.CwInvoiceRegisterMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CwInvoiceRegisterService extends BaseService {

    @Autowired
    private CwInvoiceRegisterMapper cwInvoiceRegisterMapper;

    @Autowired
    private CwInvoiceRegisterDetailService cwInvoiceRegisterDetailService;

    public Object queryInvoiceDetailPageList(Map param) {
        this.pageListInit(param);
        List<Map<String, Object>> list = cwInvoiceRegisterMapper.queryInvoiceDetailList(param);
        List humpList = DataTypeUtil.lineMapToHumpMap2(list);
        MIniUIGridResultMap resultMap = this.pageToGrid(humpList);
        return resultMap;
    }

    public List queryExcludeBillByCustomerId(String deliveryCustomerId) {
        if(DataTypeUtil.isEmptyStr(deliveryCustomerId)){
            return null;
        }
        List<Map<String, Object>> list = cwInvoiceRegisterMapper.queryExcludeBillByCustomerId(deliveryCustomerId);
        List humpList = DataTypeUtil.lineMapToHumpMap2(list);
        return humpList;
    }


    // @OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "发票-添加", operMemo = "", classType = CwInvoiceRegisterPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(CwInvoiceRegisterPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("发票添加参数错误!");
        }

        return cwInvoiceRegisterMapper.insert(pojo);
    }


    @Transactional(rollbackFor = {Exception.class})
    public int save(CwInvoiceRegisterBean bean) throws Exception {
        Date currentDate = this.getSysTime();
        String czyId = this.getLoginCzyId();

        CwInvoiceRegisterPojo pojo = bean.getCwInvoiceRegisterPojo();
        pojo.setId(this.getSysStrId());
        pojo.setCreateTime(currentDate);
        pojo.setCreateUserId(czyId);
        pojo.setUpdateTime(currentDate);
        pojo.setUpdateUserId(czyId);

        int affectRows = SpringAopUtil.getBean(this.getClass()).insert(pojo);

        if (affectRows <= SysConstant.INT_ZERO) {
            return SysConstant.INT_ZERO;
        }

        List<CwInvoiceRegisterDetailPojo> detailPojoList = bean.getDetailPojoList();
        for (CwInvoiceRegisterDetailPojo detailPojo : detailPojoList) {
            detailPojo.setId(this.getSysStrId());
            detailPojo.setCwInvoiceRegisterId(pojo.getId());
        }

        // 批量添加发发票明细
        int detailAffectRows = cwInvoiceRegisterDetailService.batchInsert(detailPojoList);

        if (detailAffectRows <= SysConstant.INT_ZERO) {
            return SysConstant.INT_ZERO;
        }


        return affectRows;
    }


}
